package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.RequiresRoles;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-20
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    @Operation(summary = "根据名字条件分页查询匹配的品牌列表")
    public TableDataInfo list(@Parameter(description = "品牌名字",example = "小米") String name)
    {
        startPage();
        List<Brand> brandList = brandService.selectBrandList(name);
        return getDataTable(brandList);
    }
    @RequiresPermissions("product:brand:query")
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询品牌详情信息")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        Brand brand = brandService.getById(id);
        if (brand == null){
            return error();
        } else {
            return success(brand);
        }
    }
    @Log(title = "品牌管理",businessType = BusinessType.INSERT)
    @RequiresPermissions("product:brand:add")
    @PostMapping
    @Operation(summary = "添加品牌")
    public AjaxResult add(@Parameter(description = "添加的品牌信息")@RequestBody Brand brand)
    {
        brand.setCreateBy(SecurityUtils.getUsername());
//        brandService.save(brand);
//        return success();
//        return brandService.save(brand) ? success() : error();
        return toAjax(brandService.save(brand));
    }
    @Log(title = "品牌管理",businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:brand:edit")
    @PutMapping
    @Operation(summary = "修改品牌")
    public AjaxResult edit(@Parameter(description = "修改的品牌信息") @RequestBody Brand brand)
    {
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));
    }
    @Log(title = "品牌管理",businessType = BusinessType.DELETE)
    @RequiresPermissions("product:brand:remove")
//    @RequiresRoles("productAdmin")
    @DeleteMapping("/{ids}")
    @Operation(summary = "根据id集合删除品牌")
    public AjaxResult remove(@Parameter(description = "品牌id",example = "[1,2]") @PathVariable List<Long> ids)
    {
        return toAjax(brandService.removeBatchByIds(ids));
    }

    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {
        return success(brandService.list());
    }


}
